<template>
  <el-dialog
      top="10vh"
      width="700px"
      title="添加/编辑供应商"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import {http} from '@/api'

export default {
  name: 'SupplierFormAdd',
  data() {
    return {
      apiKey: 'supplier',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        name: '',
        username: '',
        password: '',
        contact_person: '',
        phone: '',
        address: '',
        business_license: '',
        status: 1
      },
      rules: {
        name: [
          {required: true, message: '供应商名称不能为空', trigger: 'blur'},
          {min: 2, max: 255, message: '供应商名称长度在 2 到 255 个字符', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '登录用户名不能为空', trigger: 'blur'},
          {min: 3, max: 100, message: '用户名长度在 3 到 100 个字符', trigger: 'blur'},
          {pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '登录密码不能为空', trigger: 'blur'},
          {min: 1, max: 255, message: '密码长度在 1 到 255 个字符', trigger: 'blur'}
        ],
        contact_person: [
          {required: true, message: '联系人不能为空', trigger: 'blur'},
          {min: 2, max: 100, message: '联系人长度在 2 到 100 个字符', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '联系电话不能为空', trigger: 'blur'},
          {pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        ],
        address: [
          {max: 500, message: '地址长度不能超过500个字符', trigger: 'blur'}
        ],
        business_license: [
          {max: 255, message: '营业执照号长度不能超过255个字符', trigger: 'blur'}
        ]
      },
      formList: [
        {
          label: '供应商名称',
          prop: 'name',
          name: 'input',
          placeholder: '请输入供应商名称',
          colSpan: 12
        },
        {
          label: '登录用户名',
          prop: 'username',
          name: 'input',
          placeholder: '请输入登录用户名',
          colSpan: 12
        },
        {
          label: '登录密码',
          prop: 'password',
          name: 'input',
          type: 'password',
          placeholder: '请输入登录密码',
          colSpan: 12
        },
        {
          label: '联系人',
          prop: 'contact_person',
          name: 'input',
          placeholder: '请输入联系人姓名',
          colSpan: 12
        },
        {
          label: '联系电话',
          prop: 'phone',
          name: 'input',
          placeholder: '请输入手机号码',
          colSpan: 12
        },
        // {
        //   label: '营业执照号',
        //   prop: 'business_license',
        //   name: 'input',
        //   placeholder: '请输入营业执照号（选填）',
        //   colSpan: 12
        // },
        {
          label: '地址',
          prop: 'address',
          name: 'input',
          type: 'textarea',
          placeholder: '请输入详细地址（选填）',
          colSpan: 24,
          rows: 3
        },
        // {
        //   label: '状态',
        //   prop: 'status',
        //   name: 'switch',
        //   colSpan: 24,
        //   activeValue: 1,
        //   inactiveValue: 0,
        //   activeText: '正常',
        //   inactiveText: '禁用'
        // }
      ]
    }
  },
  methods: {
    handleFormChange({key, val}) {
      // 处理表单变化
      console.log('表单字段变化:', key, val);
    },
    open(row) {
      this.dialogValue = true
      if (row) {
        // 编辑模式
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        // 新增模式
        this.ruleForm = {
          ...this.defaultForm
        }
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, this.ruleForm).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      }).catch(error => {
        console.error('保存失败:', error);
        this.$message.error('操作失败: ' + (error.message || '未知错误'));
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style> 